package fyf.com.carddesign.main.Login;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import fyf.com.carddesign.BaseActivity;
import fyf.com.carddesign.Bll.UserBll;
import fyf.com.carddesign.Dao.DaoSession;
import fyf.com.carddesign.Dao.User;
import fyf.com.carddesign.Dao.UserDao;
import fyf.com.carddesign.MyUtl.MyApplication;
import fyf.com.carddesign.R;

/**
 * 注册页面
 * Created by Admin on 2017/10/20.
 */

public class RegisterActivity extends BaseActivity {
    private RegisterActivity _parent;
    @BindView(R.id.tbb_tv_name)
    TextView _tvTitleName;
    @BindView(R.id.r_et_name)
    EditText _etName;//用户名
    @BindView(R.id.r_et_pwd)
    EditText _etPwd;//密码
    @BindView(R.id.r_et_ok_pwd)
    EditText _etOkPwd;//确认密码
    @BindView(R.id.r_rg)
    RadioGroup _rg;
    RadioButton _rbtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _parent = RegisterActivity.this;
        ButterKnife.bind(this);
        _tvTitleName.setText("注  册");
        initSetContent();
    }

    //初始化数据内容
    private void initSetContent() {

        _rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                _rbtn = (RadioButton) group.findViewById(checkedId);
            }
        });
    }

    /**
     * 提交按钮
     *
     * @param view
     */
    @OnClick(R.id.r_btn_ok)
    public void onSubmit(View view) {
        String name = _etName.getText().toString().trim();
        String pwd = _etPwd.getText().toString().trim();
        String okPwd = _etOkPwd.getText().toString().trim();
        if (name.equals("")) {
            Toast.makeText(_parent, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (pwd.equals("")) {
            Toast.makeText(_parent, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!okPwd.equals(pwd)) {
            Toast.makeText(_parent, "两次密码输入不一致，请重新输入", Toast.LENGTH_SHORT).show();
            _etOkPwd.setText("");
            return;
        }
        if (null == _rbtn) {//使用者
            Toast.makeText(_parent, "请选择用户种类", Toast.LENGTH_SHORT).show();
            return;
        }
        if (UserBll.getInstance(_parent).selectInfo(name)) {//判断是是否存在用户名，如果存在直接return
            Toast.makeText(_parent, "用户已存在，请更换用户名", Toast.LENGTH_SHORT).show();
            return;
        }
        Byte userType = Byte.valueOf(_rbtn.getTag().toString());//获取选项
        //判断是创建管理员，还是创建用户
        if (userType == 0) {//创建管理员
            if (UserBll.getInstance(_parent).selectAdmin()) {//如果存在直接返回
                Toast.makeText(_parent, "管理员已存在，且只能创建一个管理员", Toast.LENGTH_SHORT).show();
                return;
            }
            Toast.makeText(_parent, UserBll.getInstance(_parent).addUser(name, okPwd, userType), Toast.LENGTH_SHORT).show();
            _parent.finish();
        } else {//如果是创建用户
            Toast.makeText(_parent, UserBll.getInstance(_parent).addUser(name, okPwd, userType), Toast.LENGTH_SHORT).show();
            _parent.finish();
        }
    }


    @Override
    public int getLayoutId() {
        return R.layout.register;
    }

    @OnClick(R.id.tbb_ll_back)
    public void onBack(View v) {
        _parent.finish();
    }
}
